package com.lijiajian.controller;

import com.lijiajian.Service.SaleChanceService;
import com.lijiajian.annoation.RequiredPermission;
import com.lijiajian.base.BaseController;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.pojo.SaleChance;
import com.lijiajian.query.SaleChanceQuery;
import com.lijiajian.utils.CookieUtil;
import com.lijiajian.utils.LoginUserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("saleChance")
public class SaleChanceController extends BaseController {
    @Resource
    private SaleChanceService saleChanceService;

    @RequiredPermission(code="101001")
    @GetMapping("saleChanceList")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,Integer flag,HttpServletRequest request){
        if(flag!=null&&flag==1){
            //客户开发计划查询
            //设置指派人
            int userId = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(userId);

        }
        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    @RequiredPermission(code="1010")
    @GetMapping("index")
    public String index(){
        return "saleChance/saleChance";
    }

    @RequiredPermission(code="101002")
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){
        String userName = CookieUtil.getCookieValue(request, "userName");
        saleChance.setCreateMan(userName);
        saleChanceService.addSaleChance(saleChance);
        return success("营销机会数据添加成功！");
    }

    @GetMapping("toSaleChance")
    public String toSaleChance(Integer id,HttpServletRequest request){
        if(id!=null){
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(id);
            request.setAttribute("saleChance",saleChance);
        }

        return "saleChance/addAndUpdate";
    }

    @RequiredPermission(code="101003")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){
        saleChanceService.updateSaleChance(saleChance);
        return success("更新成功");
    }

    @RequiredPermission(code="101004")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChances(Integer[] ids){
       saleChanceService.deleteSaleChances(ids);
        return success("删除成功");
    }





}
